var home = require("../../utils/util.js");
var url = home.home;
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    page:1,
    show:false,
    bgcolor: home.getKey('bgcolor'),
    list:[],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },

  zjClick() {
      wx.navigateTo({
        url: 'tourist',
      })
  },
  //查询间接客源列表
  getTouristList(){
    let that = this;
    wx.request({
      url: url + 'app/stock/stockIndiectMemberList',//改成间接客源接口
      data:{
        storeId:home.getKey('storeId'),
        highStockId:home.getKey('stockId'),
        pageNo:that.data.page,
        pageSize:100
      },
      method: 'GET',
      header: {
        "content-type": "application/json", // 默认
      },
      success:function (res) {
        if (res.data.code == 200) {
          let show = false;
          let list = that.data.list;
          if (that.data.page == 1) {
            for (let i = 0; i < res.data.result.records.length; i++) {
              res.data.result.records[i].phone = res.data.result.records[i].phone.split('');
              res.data.result.records[i].phone.splice(4,4,'****');
              res.data.result.records[i].phone = res.data.result.records[i].phone.join('');
            }
            list = res.data.result.records;
          } else {
            for (let i = 0; i < res.data.result.records.length; i++) {
              for (let i = 0; i < res.data.result.records.length; i++) {
                res.data.result.records[i].phone = res.data.result.records[i].phone.split('');
                res.data.result.records[i].phone.splice(4,4,'****');
                res.data.result.records[i].phone = res.data.result.records[i].phone.join('');
              }
              list.push(res.data.result.records[i]);
            }
          }
          if (list.length == 0) {
            show = true;
          }
          that.setData({list:list,show:show});
        } else {
          wx.showToast({
            title: '列表获取失败',
            icon: 'none'
          });
        }
      }
    })
  },

  regiser() {
    wx.showLoading({
      title: '加载中...',
    });
    let that = this;
    wx.login({
      success(res) {
        wx.request({
          url: 'https://kyz.fbhkj.com/fubaihui/app/xcx/register?storeId=' + app.globalData.storeId + '&code=' + res.code,
          data: {
            storeId: app.globalData.storeId,
            code: res.code
          },
          method: 'POST',
          header: {
            "content-type": "application/json", // 默认
          },
          success: function (rea) {
            if (rea.data.code == 200) {
              home.setKey('storeId', rea.data.result.member.storeId);
              home.setKey('stockId', rea.data.result.member.stockId);
              home.setKey('userId', rea.data.result.member.id);
              home.setKey('phone', rea.data.result.member.phone);
              home.setKey('openId', rea.data.result.member.xcxOpenId);
              home.setKey('unionId', rea.data.result.member.unionId);
              wx.hideLoading({
                success: (res) => {
                  that.getUserInfos();
                },
              })
            } else {
              home.wxMessage(rea.data.message, 'none');
            }
          }
        })
      }
    })
  },

  //获取用户数据
  getUserInfos() {
    let that = this;
    wx.request({
      url: url + 'app/getUserInfo',
      data: {
        storeId: home.getKey('storeId'),
        phone: home.getKey('phone'),
        xcxOpenId: home.getKey('openId'),
      },
      method: 'GET',
      header: {
        "content-type": "application/json", // 默认
      },
      success: function (res) {
        if (res.data.code == 200) {
          if (res.data.result.discount == null) {
            res.data.result.discount = 10
          }
          if (res.data.result.balance == null) {
            res.data.result.balance = 0;
          }
          if (res.data.result.stockLowest == null) {
            res.data.result.stockLowest = 0;
          }
          if (res.data.result.equityBalance == null) {
            res.data.result.equityBalance = 0;
          }
          if (res.data.result.totalDividend == null) {
            res.data.result.totalDividend = 0;
          }
          if (res.data.result.equityTotal == null) {
            res.data.result.equityTotal = 0;
          }
          home.setKey('discount', res.data.result.discount);
          home.setKey('equityBalance', res.data.result.equityBalance);
          home.setKey('balance', res.data.result.balance);
          home.setKey('levelName', res.data.result.levelName);
          home.setKey('stockLowest', res.data.result.stockLowest);
          home.setKey('stockId', res.data.result.stockId);
          home.setKey('birthday', res.data.result.birthday);
          home.setKey('equityTotal', res.data.result.equityTotal);
          home.setKey('nickName', res.data.result.nickName);
          home.setKey('canWithdrawal', res.data.result.canWithdrawal);
          home.setKey('noUseEquityBalancd', res.data.result.noUseEquityBalancd);
          home.setKey('avatar', res.data.result.headProtrait);
          that.getTouristList();
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.regiser();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let page = this.data.page;
    this.setData({page:++page});
    this.getTouristList();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})